package com.hansen.library.ui.widget.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.StringRes;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.hansen.library.R;

/**
 * Created by Administrator on 2017/5/19 0019.
 */
public class LoadingDialog {
    private Context mContext;

    private AlertDialog mAlertDialog;

    private AppCompatImageView iv_progress_spinner;
    //标题
    private AppCompatTextView tv_wait_msg;

    private Window mWindow;

    private AnimationDrawable adProgressSpinner;

    public LoadingDialog(Context mContext) {
        this.mContext = mContext;
    }

    public LoadingDialog builder() {
        mAlertDialog = new AlertDialog.Builder(mContext).create();

        mAlertDialog.show();

        mWindow = mAlertDialog.getWindow();

        //FLAG_NOT_FOCUSABLE: 让window不能获得焦点
        //这样用户快就不能向该window发送按键事，即屏蔽掉onKey事件。
        //FLAG_ALT_FOCUSABLE_IM: 隐藏软键盘
        /*mWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);*/
        //mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE);

        View contentView = LayoutInflater.from(mContext)
                .inflate(R.layout.dialog_loading, null);

        mWindow.setBackgroundDrawableResource(R.drawable.shape_material_dialog_window);

        mWindow.setContentView(contentView);

        iv_progress_spinner = contentView.findViewById(R.id.iv_progress_spinner);

        tv_wait_msg = contentView.findViewById(R.id.tv_wait_msg);

        iv_progress_spinner.setImageResource(R.drawable.anim_round_spinner_fade);

        startAnim();

        return this;
    }

    public void show() {
        if (mAlertDialog != null) {
            mAlertDialog.show();

            startAnim();
        }
    }

    private void startAnim() {
        if(iv_progress_spinner != null) {
            if (adProgressSpinner == null) {
                adProgressSpinner = (AnimationDrawable) iv_progress_spinner.getDrawable();
            }
            adProgressSpinner.start();
        }
    }

    public void dismiss() {
        if (mAlertDialog != null && mAlertDialog.isShowing()) {
            mAlertDialog.dismiss();
        }

        if(adProgressSpinner != null && adProgressSpinner.isRunning()) {
            adProgressSpinner.stop();
            adProgressSpinner = null;
        }
    }


    public LoadingDialog setTitle(@StringRes int resId) {
        if (tv_wait_msg != null) {
            tv_wait_msg.setText(resId);
        }

        return this;
    }


    public LoadingDialog setTitle(CharSequence title) {
        if(tv_wait_msg != null) {
            tv_wait_msg.setText(title);
        }

        return this;
    }

    public LoadingDialog setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        if(mAlertDialog != null) {
            mAlertDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        }

        return this;
    }

    public LoadingDialog setCanceled(boolean canceled) {
        if(mAlertDialog != null) {
            mAlertDialog.setCancelable(canceled);
        }

        return this;
    }

    public boolean isShowing() {
        if (mAlertDialog != null) {
            return mAlertDialog.isShowing();
        }
        return false;
    }
}

